package com.apes.fn.fico.settleExpense;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.JpaUtil;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.service.PartyRoleService;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

/**
 * 结算费用单
 * 用于记录在结算的时候生成的费用（平台服务费、信息费等）
 * @author wuq
 * @create 2020-04-10 14:09
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SettleExpense.CONST_TABLE_NAME)
public class SettleExpense extends BaseModel {
    public static final String CONST_TABLE_NAME = "fn_settle_expense";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "结算费用单")
    @Comment("结算费用单号")
    private String id; //单号

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "payment_id", referencedColumnName = "id")
    @Comment("公司编码(收/付款方)")
    private PartyRole payment; //收付款方（类型为 out 时为付款方， in 时为收款方）

    @Comment("部门")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    private Dept dept;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "tenant_id", referencedColumnName = "id")
    @Comment("公司编码")
    private PartyRole tenant; // 公司

    @Column(name = "loss_Quotation_Amt", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("定损金额")
    private double lossQuotationAmt = 0; //定损金额

    @Column(name = "amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("付款金额")
    private double amount = 0; //付款金额

    @Comment("类型（out:支出;in:收入）")
    @Column(name = "type", length = 30)
    private String type;

    @Comment("平台返还标记")
    @Column(name = "platform_return", length = 2)
    private int platformReturn = 0; // 平台代收，需要返还给门店

    @Comment("返还类型（service:工时返还;parts:配件返还）")
    @Column(name = "back_type", length = 16)
    private String backType;

    @States(name = "费用类型", values = {
            @State(id = "infoExpense", name = "定损介绍费"),
            @State(id = "platformExpense", name = "平台管理费"),
            @State(id = "lossQuotationExpense", name = "定损管理费"),
            @State(id = "innerInfoExpense", name = "内部定损介绍费")
    })
    @Comment("费用类型（接案介绍费;平台管理费;定损服务费;内部定损介绍费）")
    @Column(name = "expense_type", length = 30)
    private String expenseType;

    @Column(name = "car_license", length = 64)
    @Comment("车牌号码")
    private String carLicense;

    @Comment("案件类型")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "case_type", referencedColumnName = "id")
    private Enumeration caseType;

    @Comment("源单据")
    @Column(name = "origin", length = 30)
    private String origin;

    @Comment("源单据类型")
    @Column(name = "origin_Business_Type", length = 30)
    private String originBusinessType;

    @Column(name = "source", length = 30)
    @Comment("来源单据")
    private String source;

    @Column(name = "source_business_type", length = 32)
    @Comment("来源单据类型")
    private String sourceBusinessType;

    @Comment("备注")
    @Column(name = "remark", length = 120)
    private String remark;

    /**
     * 平台公司
     **/
    @Transient
    private PartyRole platformCompany;

    public PartyRole getPlatformCompany() {
        if (platformCompany != null) return platformCompany;
        platformCompany = (PartyRole) JpaUtil.getImplementation(SpringManager.getBean(PartyRoleService.class).findOne("1007", "Company"));
        platformCompany = MapUtil.getO(MapUtil.mapped(platformCompany), PartyRole.class);
        return  platformCompany;
    }

}
